package com.synchrona.dataProcessor;

import java.io.File;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.events.XMLEvent;

import com.ctc.wstx.stax.WstxInputFactory;
import com.synchrona.common.EnumService;
import com.synchrona.update.Update;
import com.synchrona.user.User;

import org.codehaus.stax2.XMLInputFactory2;

public class DMMLParser {

	//Parser for XML data
	private WstxInputFactory xmlInputFactory = null;
	private XMLStreamReader xmlStreamReader = null;
	private XMLStreamWriter xmlStreamWriter = null;

	//Returns the update object for the parsed XML data
	public Update parseXMLData(Update update) throws XMLStreamException {
		xmlInputFactory = new WstxInputFactory();

		xmlInputFactory.setProperty(
				XMLInputFactory2.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
		xmlInputFactory
				.setProperty(XMLInputFactory2.IS_SUPPORTING_EXTERNAL_ENTITIES,
						Boolean.FALSE);
		xmlInputFactory.setProperty(XMLInputFactory2.IS_COALESCING,
				Boolean.FALSE);
		xmlInputFactory.setProperty(XMLInputFactory2.IS_VALIDATING,
				Boolean.FALSE);

		xmlInputFactory.configureForSpeed();
		XMLStreamReader xmlStreamReader = xmlInputFactory
				.createXMLStreamReader(new StringReader(update.getData()));

		int eventType = xmlStreamReader.getEventType();
		String currentElement = "";
		String currentElementText = "";
		String statusMessage = "";
		System.out.println("Parsing data........");
		while (xmlStreamReader.hasNext()) {
			eventType = xmlStreamReader.next();
			switch (eventType) {
			case XMLEvent.START_ELEMENT:
				currentElement = xmlStreamReader.getName().toString();
				if (currentElement.equals("Element")) {
					String attributeName = xmlStreamReader.getAttributeValue(
							"", "elementName");
					String attributeValue = xmlStreamReader.getElementText();
					if (attributeName.equals("Site")) {
						if (attributeValue.contains("facebook")) {
							update.setService(EnumService.Facebook);
						} else if (attributeValue.contains("twitter")) {
							update.setService(EnumService.Twitter);
						} else if (attributeValue.contains("blogger")) {
							update.setService(EnumService.Blogger);
						} else if (attributeValue.contains("moodle")) {
							update.setService(EnumService.Moodle);
						}
					}
					if (attributeName.equals("TargetProfile")) {
						update.setUpdateType(attributeValue);
					}
					if (attributeName.equals("UpdateData")) {
						update.setData(attributeValue);
						update.setUpdateType("status");
					}
				}
				break;
			}
		}
		return update;
	}
	//Returns the Task object for the XML data
	public Task parseXML(String dataString) throws XMLStreamException {
		xmlInputFactory = new WstxInputFactory();

		xmlInputFactory.setProperty(
				XMLInputFactory2.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
		xmlInputFactory
				.setProperty(XMLInputFactory2.IS_SUPPORTING_EXTERNAL_ENTITIES,
						Boolean.FALSE);
		xmlInputFactory.setProperty(XMLInputFactory2.IS_COALESCING,
				Boolean.FALSE);
		xmlInputFactory.setProperty(XMLInputFactory2.IS_VALIDATING,
				Boolean.FALSE);

		xmlInputFactory.configureForSpeed();

		List tasks = new LinkedList();
		HashMap map = new HashMap();
		Task task = new Task();
		XMLStreamReader xmlStreamReader = xmlInputFactory
				.createXMLStreamReader(new StringReader(dataString));

		int eventType = xmlStreamReader.getEventType();
		String currentElement = "";
		String currentElementText = "";

		while (xmlStreamReader.hasNext()) {
			eventType = xmlStreamReader.next();
			switch (eventType) {
			case XMLEvent.START_ELEMENT:
				currentElement = xmlStreamReader.getName().toString();
				System.out.println(currentElement);
				if (currentElement.equals("Element")) {
					String attributeName = xmlStreamReader.getAttributeValue(
							"", "elementName");
					String attributeValue = xmlStreamReader.getElementText();
					if (attributeName.equals("Site")) {
						System.out.println("Got The Site Element!!!...");
						if (attributeValue.contains("facebook")) {
							task.setSiteName("fb");
							task.setSiteURL(attributeValue);
						} else if (attributeValue.contains("twitter")) {
							task.setSiteName("tw");
							task.setSiteURL(attributeValue);
						} else if (attributeValue.contains("blogger")) {
							task.setSiteName("bg");
							task.setSiteURL(attributeValue);
						} else if (attributeValue.contains("moodle")) {
							task.setSiteName("md");
							task.setSiteURL(attributeValue);
						}
					}
					map.put(attributeName, attributeValue);

				}
				break;
			}
			task.setHashMap(map);
		}
		return task;
	}

	

}
